package com.lanou3g.springcloud.zuul.web;

import com.lanou3g.springcloud.zuul.bean.CalcResult;
import com.lanou3g.springcloud.zuul.client.CalcClient;
import com.lanou3g.springcloud.zuul.client.HelloClient;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
public class CalcWithNameController {

	@Autowired
	private HelloClient helloClient;

	@Autowired
	private CalcClient calcClient;

	@RequestMapping("/calc/{name}/{numA}/{numB}/{operator}")
	public CalcResult calc(@PathVariable String name, @PathVariable Integer numA, @PathVariable Integer numB, @PathVariable Integer operator) {
		CalcResult calcResult = calcClient.calc(numA, numB, operator);
		String helloResult = helloClient.helloName(name);
		try {
			if(StringUtils.isNotBlank(helloResult)) {
				String _name = helloResult.split(",")[1].trim();
				calcResult.setName(_name);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return calcResult;
	}
}
